﻿using NLog;
using System;
using System.Text;
using Yz.Model.Constants;

namespace Yz.Core.Tools
{
    public class YzNLog
    {

        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

        #region 普通级别

        /// <summary>
        /// 普通级别
        /// </summary>
        /// <param name="content"></param>
        public static void Info(string content)
        {
            if (Config.EnableInfoLog)
            {
                Logger.Info(content);
            }
        }

        /// <summary>
        /// 普通级别
        /// </summary>
        /// <param name="exception"></param>
        /// <param name="content"></param>
        public static void Info(Exception exception)
        {
            string msg = GetMessageFromExcetion(exception);
            Info(msg);
        }
        /// <summary>
        /// 普通级别
        /// </summary>
        /// <param name="exception"></param>
        /// <param name="content"></param>
        public static void Info(Exception exception, string content)
        {
            string msg = GetMessageFromExcetion(exception, content);
            Info(msg);
        }
        #endregion

        #region 警告级别

        /// <summary>
        /// 警告级别
        /// </summary>
        /// <param name="content"></param>
        public static void Warn(string content)
        {
            Logger.Warn(content);
        }

        /// <summary>
        /// 警告级别
        /// </summary>
        /// <param name="exception"></param>
        /// <param name="content"></param>
        public static void Warn(Exception exception)
        {
            string msg = GetMessageFromExcetion(exception);
            Warn(msg);
        }
        /// <summary>
        /// 警告级别
        /// </summary>
        /// <param name="exception"></param>
        /// <param name="content"></param>
        public static void Warn(Exception exception, string content)
        {
            string msg = GetMessageFromExcetion(exception, content);
            Warn(msg);
        }
        #endregion

        #region 错误级别

        /// <summary>
        /// 错误级别
        /// </summary>
        /// <param name="content"></param>
        public static void Error(string content)
        {
            Logger.Error(content);
        }

        /// <summary>
        /// 错误级别
        /// </summary>
        /// <param name="content"></param>
        public static void Error(Exception exception)
        {
            string msg = GetMessageFromExcetion(exception);
            Error(msg);
        }

        /// <summary>
        /// 错误级别
        /// </summary>
        /// <param name="exception"></param>
        /// <param name="content"></param>
        public static void Error(Exception exception, string content)
        {
            string msg = GetMessageFromExcetion(exception, content);
            Error(msg);
        }

        #endregion

        #region 致命级别

        public static void Fatal(string content)
        {
            Logger.Fatal(content);
        }
        /// <summary>
        /// 错误级别
        /// </summary>
        /// <param name="content"></param>
        public static void Fatal(Exception exception)
        {
            string msg = GetMessageFromExcetion(exception);
            Fatal(msg);
        }
        public static void Fatal(Exception exception, string content)
        {
            string msg = GetMessageFromExcetion(exception, content);
            Fatal(msg);
        }
        #endregion

        #region 调试级别
        public static void Debug(string content)
        {
            if (Config.EnableDebugLog)
            {
                Logger.Debug(content);
            }
        }
        public static void Debug(Exception exception)
        {
            string msg = GetMessageFromExcetion(exception);
            Debug(msg);
        }

        public static void Debug(Exception exception, string content)
        {
            string msg = GetMessageFromExcetion(exception, content);
            Debug(msg);
        }
        #endregion

        #region Trace级别
        public static void Trace(string content)
        {
            Logger.Trace(content);
        }
        public static void Trace(Exception exception)
        {
            string msg = GetMessageFromExcetion(exception);
            Trace(msg);
        }
        public static void Trace(Exception exception, string content)
        {
            string msg = GetMessageFromExcetion(exception, content);
            Trace(msg);
        }
        #endregion

        /// <summary>
        /// 从异常中获得异常详情
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        private static string GetMessageFromExcetion(Exception ex, string content = "")
        {
            StringBuilder strb = new StringBuilder();
            if (!string.IsNullOrEmpty(content))
            {
                strb.AppendLine(content);
            }
            strb.AppendLine(ex.Source);
            strb.AppendLine(ex.Message);
            if (ex.InnerException != null)
            {
                strb.AppendLine(ex.InnerException.Message);
                strb.AppendLine(ex.InnerException.StackTrace);
            }
            strb.AppendLine(ex.StackTrace);
            return strb.ToString();
        }
    }
}
